<template>
  <a-modal :visible="isFaultReport" @cancel="cloFaultReport" @ok="handleOk" title="故障上传"
           :confirm-loading="confirmLoading" centered width="50%" :body-style="{minHeight:'50vh'}" destroyOnClose>
    <a-form-model ref="ruleForm" :rules="rules" :model="faultFrom" :label-col="labelCol" :wrapper-col="wrapperCol"
                  :colon="false">
      <a-form-model-item label="企业名称">
        <a-input v-model="faultFrom.companyName" disabled/>
      </a-form-model-item>
      <a-form-model-item label="站点名称">
        <a-input v-model="faultFrom.monitorName" disabled/>
      </a-form-model-item>
      <a-form-model-item label="故障因子" prop="monitorBreakdownDataDtoList">
        <a-checkbox-group v-model="faultFrom.monitorBreakdownDataDtoList">
          <a-checkbox :value="v" name="type" v-for="(v,i) in checkboxData" :key="i">
            {{ v.monitorFactorsName }}
          </a-checkbox>
        </a-checkbox-group>
      </a-form-model-item>
    </a-form-model>

  </a-modal>
</template>

<script>
export default {
  name: "FaultReport",
  data() {
    return {
      confirmLoading: false,
      isFaultReport: false,
      faultData: {},
      checkboxData: [],  // 多选数组
      labelCol: {span: 4},
      wrapperCol: {span: 18},
      faultFrom: {
        companyName: "",  // 企业名称
        monitorName: "",
        monitorBreakdownDataDtoList: [],
      },
      rules: {
        monitorBreakdownDataDtoList: [{type: 'array', required: true, message: "故障因子必选", trigger: 'change'}]
      }
    }
  },
  methods: {
    cloFaultReport() {
      this.isFaultReport = false;
      this.faultData = {};
      this.checkboxData = [];
    },
    handleOk() {
      this.$refs.ruleForm.validate(valid => {
        if (valid) {
          this.confirmLoading = true;
          let obj = {
            companyName: this.faultData.companyName,
            monitorId: this.faultData.id,
            monitorName: this.faultData.monitorName,
            mn: this.faultData.mn,
            monitorBreakdownDataDtoList: [],
          }
          this.faultFrom.monitorBreakdownDataDtoList.forEach(v => {
            let o = {
              data: v.unit ? v.realValue + v.unit : v.realValue,
              time: v.datetime,
              monitorFactorsName: v.monitorFactorsName
            };
            obj.monitorBreakdownDataDtoList.push(o)
          })
          this.$api.monitorErrorSave(obj).then(res => {
            if (res.data.data === true) {
              this.confirmLoading = false;
              this.isFaultReport = false;
              this.$message.info(res.data.errorMessage)
              this.faultFrom = {
                companyName: "",  // 企业名称
                monitorName: "",
                monitorBreakdownDataDtoList: [],
              }
            }else {
              this.confirmLoading = false;
              this.$message.warning("故障因子已经上报")
            }
          })
        }
      })
    },
    // 初始化数据
    init() {
      this.faultData.monitorFactorsPos.forEach(v => {
        if (v.dataStatus === "D") {
          this.checkboxData.push(v)
          this.faultFrom.monitorBreakdownDataDtoList.push(v.monitorFactorsCode)
        }
      })
      this.faultFrom = {
        companyName: this.faultData.companyName,  // 企业名称
        monitorName: this.faultData.monitorName,
      };
    }
  },
  mounted() {

  },

}
</script>

<style scoped lang="scss">

</style>